import os

import cv2
from tqdm import tqdm

from option import parse_args

if __name__ == '__main__':
    args = parse_args()
    version = args.compression_version
    original_crops = os.path.join(args.root_dir, 'original_sequences', 'actors', version, 'crops')
    fake_crops = os.path.join(args.root_dir, 'manipulated_sequences', 'DeepFakeDetection', version, 'crops')
    videos = open(f'abnormal_width_statistics_{version}.dat').readlines()
    for video_name in tqdm(videos):
        video_split = video_name.split(',')
        assert len(video_split) == 3
        ori_video_path = os.path.join(original_crops, video_split[0])
        if os.path.exists(ori_video_path):
            video_path = ori_video_path
        else:
            video_path = os.path.join(fake_crops, video_split[0])

        max_width = int(video_split[2].strip())
        for img_name in os.listdir(video_path):
            img = cv2.imread(os.path.join(video_path, img_name), cv2.IMREAD_GRAYSCALE)
            diff = max_width - img.shape[1]
            if img.shape[1] < 100 and diff > 50:
                os.remove(os.path.join(video_path, img_name))
